package cn.com.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiGoods;
import cn.com.api.entity.ApiU_advert;
import cn.com.api.entity.ApiU_circle;
import cn.com.api.entity.ApiU_discover;
import cn.com.api.entity.ApiU_favorite;
import cn.com.api.entity.U_article;
import cn.com.api.service.ApiGoodsService;
import cn.com.api.service.ApiT_productsService;
import cn.com.api.service.ApiU_advertService;
import cn.com.api.service.ApiU_circleService;
import cn.com.api.service.ApiU_discoverService;
import cn.com.api.service.ApiU_favoriteService;
import cn.com.api.service.CheckService;
import cn.com.api.service.U_articleService;
import cn.com.util.JsonUtil;
import cn.com.util.PageInfo;
import cn.com.util.Util;
import cn.com.util.Utils;

@Controller
@RequestMapping(Utils.SYS_API + "favorite/")
public class ApiU_FavoriteController {

	@Autowired
	private ApiU_discoverService apiU_discoverService;
	@Autowired
	private ApiU_favoriteService apiU_favoriteService;
	@Autowired
	private CheckService checkService;
	@Autowired
	private ApiU_advertService apiU_advertService;
	@Autowired
	private U_articleService u_articleService;
	@Autowired
	private ApiU_circleService apiU_circleService;
	@Autowired
	private ApiGoodsService apiGoodsService;
	@Autowired
	private ApiT_productsService apiproductservice;

	private static Logger logger = Logger.getLogger(ApiU_orgController.class);

	// /**
	// * 收藏和取消文章分类
	// *
	// * @param addcateids
	// * 要收藏的分类id集合，多个用,分割
	// * @param delcateids
	// * 要取消收藏的分类id集合，多个用,分割
	// * @param user_id
	// * 用户id
	// * @param token
	// *
	// * @return
	// */
	// @RequestMapping(value = "addOrDelArticleCates")
	// public void addOrDelArticleCates(HttpServletRequest request,
	// HttpServletResponse response, String addcateids,
	// String delcateids, String user_id, String token) throws Exception {
	// Map<String, Object> map = new HashMap<String, Object>();
	// try {
	// // 添加收藏
	// if (addcateids != null && !"".equals(addcateids)) {
	// String[] addids = addcateids.split(",");
	// for (String id : addids) {
	// ApiU_favorite apiU_favorite = new ApiU_favorite();
	// apiU_favorite.setConn_id(Integer.valueOf(id));
	// apiU_favorite.setType(3);
	// apiU_favorite.setUser_id(Integer.valueOf(user_id));
	// apiU_favorite.setAdd_time(DateUtil.nowDateToString());
	// apiU_favoriteService.insertU_favorite(apiU_favorite);
	// }
	// }
	// // 删除收藏收藏
	// if (delcateids != null && !"".equals(delcateids)) {
	// String[] delids = delcateids.split(",");
	// for (String id : delids) {
	// Map param = new HashMap();
	// param.put("conn_id", id);
	// param.put("type", 3);
	// param.put("user_id", user_id);
	// apiU_favoriteService.deleteU_favoriteByMap(param);
	// }
	// }
	// map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
	// map.put(Utils.MESSAGE, "success");
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
	// map.put(Utils.MESSAGE, "系统异常");
	// }
	// JsonUtil.printJson(response, JsonUtil.object2json(map));
	// }

	/**
	 * 收藏和取消收藏
	 * 
	 * @param conn_id
	 *            关联id
	 * @param status
	 *            收藏还是取消收藏的状态（1-收藏；0-取消收藏）
	 * @param type
	 *            收藏和取消收藏的类型(类型 0 社群， 1特卖会， 2新发现， 3文章分类， 4红包广告， 5文章，6位置广告)
	 * @param user_id
	 *            用户id
	 * @param token
	 * 
	 * @return
	 */
	@RequestMapping(value = "addOrDelFavorite")
	public void addOrDelFavorite(HttpServletRequest request, HttpServletResponse response, String conn_id,
			String status, String user_id, String type, String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {

			Map param = new HashMap();
			param.put("conn_id", conn_id);
			param.put("type", Integer.valueOf(type));
			param.put("user_id", user_id);
			apiU_favoriteService.deleteU_favoriteByMap(param);
			// 添加收藏
			if ("1".equals(status)) {
				ApiU_favorite apiU_favorite = new ApiU_favorite();
				apiU_favorite.setConn_id(Integer.valueOf(conn_id));
				apiU_favorite.setType(Integer.valueOf(type));
				apiU_favorite.setUser_id(Integer.valueOf(user_id));
				apiU_favorite.setAdd_time(Util.CurrentTime());
				apiU_favoriteService.insertU_favorite(apiU_favorite);
			}
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "操作完成");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 我的-收藏(获取我收藏的所有项目目录和数量)
	 * 
	 * @param user_id
	 *            用户id
	 * @param token
	 * 
	 * @return
	 */
	@RequestMapping(value = "MyFavoriteSummary")
	public void MyFavoriteSummary(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> params = new HashMap<String, String>();
			params.put("u_id", user_id);
			params.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(params, map)) {
				map.clear();
				map = apiU_favoriteService.MyFavoriteSummary(user_id);

				// List<Map<String, Object>> resultMap =
				// this.apiU_favoriteService
				// .getMyFavoriteSummary(Integer.valueOf(user_id));
				// map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				// map.put(Utils.MESSAGE, "操作完成");
				// map.put(Utils.DATAS, resultMap);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 我的-收藏-具体收藏列表
	 * 
	 * @param type
	 *            (类型 0 社群， 1特卖会， 2新发现， 3文章分类， 4红包广告, 5文章，6位置广告)
	 * 
	 * @param user_id
	 *            用户id
	 * @param token
	 * 
	 * @param currentPage
	 *            页数
	 * @param showCount
	 *            每页显示数
	 * @return
	 */
	@RequestMapping(value = "MyFavoriteListByType")
	public void MyFavoriteListByType(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String type, PageInfo page) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> params = new HashMap<String, String>();
			String cate_type = request.getParameter("cate_type");

			params.put("u_id", user_id);
			params.put("token", token);
			params.put("type", type);

			// if (checkService.checkToken(user_id, token, map) &&
			// checkService.checkParams(params, map)) {
			switch (type) {
			case "5":// 文章/头条
				Map param = new HashMap();
				param.put("user_id", user_id);
				List<U_article> u_articles = u_articleService.getArticleFavoriteByUserID(param, page);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成");
				map.put(Utils.DATAS, u_articles);
				map.put(Utils.Page, page);
				break;

			case "4":
			case "6":// 红包广告&位置广告
				List<ApiU_advert> advList = this.apiU_advertService.findAdvertListByFavoriteType(user_id,
						Integer.valueOf(type), page);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成");
				map.put(Utils.DATAS, advList);
				map.put(Utils.Page, page);
				break;
			case "0": // 社群
				List<ApiU_circle> circle_list = this.apiU_circleService.findFavoriteCircleList(user_id,
						Integer.valueOf(type), page);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成");
				map.put(Utils.DATAS, circle_list);
				map.put(Utils.Page, page);
				break;

			case "1": // 特卖会
				List<ApiGoods> apigoodsList = this.apiGoodsService.findGoodsList(user_id, Integer.valueOf(type), page);
				for (ApiGoods apiGoods : apigoodsList) {
					apiGoods.setProductList(apiproductservice.getUndeletedProductsByGoodsId(apiGoods.getGoods_id()));
				}
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成");
				map.put(Utils.DATAS, apigoodsList);
				map.put(Utils.Page, page);
				break;

			case "2": // 新发现
				// List<ApiU_discover> discover_list = new
				// ArrayList<ApiU_discover>();
				List<ApiU_discover> discover_list = new ArrayList<ApiU_discover>();
				if (cate_type != null && !cate_type.equals("")) {
					String[] cate = cate_type.split(",");
					for (String string : cate) {
						List<ApiU_discover> discover = this.apiU_discoverService.findFavoriteDiscoverList(string,
								user_id, Integer.valueOf(type), page);
						for (ApiU_discover apiU_discover : discover) {
							discover_list.add(apiU_discover);
						}
					}
				} else {
					discover_list = this.apiU_discoverService.findFavoriteDiscoverList("", user_id,
							Integer.valueOf(type), page);
				}
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成");
				map.put(Utils.DATAS, discover_list);
				map.put(Utils.Page, page);
				break;

			default:
				map.put(Utils.RESULT, "0003");
				map.put(Utils.MESSAGE, "分类不存在！");
				break;
			}
			// }

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.objectToJsonStr(map));
	}
}
